package com.cms.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cms.common.StringUtils;
import com.cms.constant.CmsConstant;
import com.cms.model.TrAdminUser;
import com.cms.model.TrPageCatalog;
import com.cms.model.TrPageContent;
import com.cms.service.PageCatalogService;
import com.cms.service.PageContentService;
import com.cms.service.ReportService;
import com.google.gson.Gson;

@Controller
public class PageContentController extends BaseController {
	@Autowired
	private PageCatalogService pageCatalogService;

	@Autowired
	private PageContentService pageContentService;

	@Autowired
	private ReportService reportService;

	/***
	 * 
	 * @param req
	 * @param resp
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/pageContent.do")
	public String enterPageContent(HttpServletRequest req,
			HttpServletResponse resp, ModelMap map) throws Exception {
		TrPageCatalog catalog = new TrPageCatalog();
		map.put("pageCatalogList", this.pageCatalogService
				.queryPageCatalog(catalog));
		return "pageContent/pageContentMain.html";
	}

	@RequestMapping("/pageContentList.do")
	public String pageContentList(HttpServletRequest req,
			HttpServletResponse resp, ModelMap map) throws Exception {
		Map<String, Object> param = this.buildQueryMap(req);
		map.put("pageContentList", this.reportService.pagingReportResult(
				"pagingPageContent", param));
		return "pageContent/pageContentList.html";
	}

	@RequestMapping("/addPageContent.do")
	public String addPageContent(HttpServletRequest req,
			HttpServletResponse resp, ModelMap map) throws Exception {
		TrPageCatalog catalog = new TrPageCatalog();
		TrPageContent content = new TrPageContent();
		catalog.setCatalogStatus("publish");
		map.put("pageContent", this.pageCatalogService
				.queryPageCatalog(catalog));
		map.put("content", content);
		map.put("action", "savePageContent.do");
		return "pageContent/pageContentInfo.html";
	}

	@RequestMapping("/savePageContent.do")
	public String savePageContent(HttpServletRequest req,
			HttpServletResponse resp, ModelMap map) throws Exception {
		TrAdminUser tu = this.getLoginUser(req);
		TrPageContent content = new TrPageContent();
		content.setCreateUser(tu.getId());
		content.setModifyUser(tu.getId());
		this.export(req, "pg", content);
		this.pageContentService.savePageContent(content);
		return "redirect:/pageContent.do";
	}

	@RequestMapping("/toEditPageContent.do")
	public String toEditPageContent(HttpServletRequest req,
			HttpServletResponse resp, ModelMap map) throws Exception {
		TrPageCatalog catalog = new TrPageCatalog();
		catalog.setCatalogStatus("publish");
		map.put("pageContent", this.pageCatalogService
				.queryPageCatalog(catalog));
		TrPageContent tc = new TrPageContent();
		tc.setId(StringUtils.str2Int(req.getParameter("pageContentId")));
		map.put("content", this.pageContentService.getPageContent(tc));
		map.put("action", "updatePageContent.do");
		return "pageContent/pageContentInfo.html";
	}

	@RequestMapping("/updatePageContent.do")
	public String updatePageContent(HttpServletRequest req,
			HttpServletResponse resp, ModelMap map) throws Exception {
		TrAdminUser tu = this.getLoginUser(req);
		TrPageContent content = new TrPageContent();
		content.setModifyUser(tu.getId());
		this.export(req, "pg", content);
		this.pageContentService.updatePageContent(content);
		return "redirect:/pageContent.do";
	}

	@RequestMapping("/deletePageContent.do")
	public void deletePageContent(HttpServletRequest req,
			HttpServletResponse resp, ModelMap map) throws Exception {
		HashMap<String, Object> result = new HashMap<String, Object>();
		try {
			TrPageContent content = new TrPageContent();
			content.setId(StringUtils.str2Int(req.getParameter("pageContentId")));
			pageContentService.deletePageContent(content);
			result.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
		} catch (Exception e) {
			result.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
			result.put(CmsConstant.MSG, e.getMessage());
		}
		this.renderText(resp, new Gson().toJson(result));
	}

}
